﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Blazing.BusinessObjects.Widgets;
using Blazing.BusinessObjects.Cms;

namespace Blazing.UI.Cms.Admin.Usercontrols
{
    public partial class SelectPageWidget : System.Web.UI.UserControl
    {
        public Blazing.BusinessObjects.Widgets.PageWidget PageWidget
        {
            get
            {
                PageWidget w = new PageWidget();
                //if (ViewState["PageId"] != null)
                //{
                //    Contentpage p = Contentpage.Load((int)ViewState["PageId"]);
                //    foreach (Blazing.BusinessObjects.Widgets.PageWidget pw in p.Widgets)
                //    {
                //        if (pw.ID == (int)ViewState["ID"])
                //        {
                //            w = pw;
                //        }
                //    }
                //}

                if (ViewState["PageId"] != null)
                    w.Page = Contentpage.Load((int)ViewState["PageId"]); 
                if (ViewState["ID"] != null)
                    w.ID = (int)ViewState["ID"];
                w.Widget = Widget.Load(int.Parse(ddlWidget.SelectedValue));
                w.Region = WidgetRegion.Load(int.Parse(ddlWidgetRegion.SelectedValue));
                w.Parameters = TextBoxParameters.Text;
                return w;
            }
            set
            {
                ddlWidget.SelectedValue = value.Widget.ID + "";
                ddlWidgetRegion.SelectedValue = value.Region.ID + "";
                TextBoxParameters.Text = value.Parameters;
                LiteralWidgetName.Text = string.Format("<b>Widget:</b> {0}", value.Widget.Name);
                LiteralWidgetRegion.Text = string.Format("<b>Region:</b> {0}", value.Region.Name);

                if (!value.IsNull("ID"))
                {
                    ViewState["ID"] = value.ID;
                }
                if (value.Page != null)
                {
                    ViewState["PageId"] = value.Page.ID;
                }
            }
        }

        public bool Enabled
        {
            set
            {
                ddlWidget.Visible = value;
                ddlWidgetRegion.Visible = value;
                LiteralWidgetName.Visible = !value;
                LiteralWidgetRegion.Visible = !value;
                TextBoxParameters.Enabled = value;
                LinkButtonRemove.Visible = !value;
            }
        }

        public bool IsActive
        {
            get
            {
                if (ViewState["IsActive"] != null)
                {
                    return (bool)ViewState["IsActive"];
                }
                return true;
            }
            set { ViewState["IsActive"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LinkButtonRemove_Click(object sender, EventArgs e)
        {
            IsActive = false;
            Visible = false;
        }
    }
}